<?php

require_once('config.php');
require_once('functions/general.php');
require_once('functions/sql.php');
require_once('sql/sql_profile.php');
require_once('Smarty.class.php');
session_start();
$smarty = new Smarty;
$smarty->assign('CONFIG',$CONFIG);
$smarty->template_dir = array('templates/common', 'templates/profile');

$logged_in = logged_in_status($smarty);
$mysql_link = get_mysql_link();
	
if ($logged_in && $_POST) {

    $profile = new Sql_profile($mysql_link);

    if ($profile->update_profile($_SESSION[user], $_POST));
    else ($smarty->assign('passchange_failed', true));

    $smarty->assign('profile', $profile->get_profile($_SESSION[user]));
    $smarty->display('profile.tpl');


}

else if ($logged_in && !$_GET) {
    $profile = new Sql_profile($mysql_link);
    $smarty->assign('profile', $profile->get_profile($_SESSION['user']));
    $smarty->display('profile.tpl');

}

else if($_GET[user]) {
    //Display a users profile
    $profile = new Sql_profile($mysql_link);
    $profile_data = $profile->get_profile($_GET[user]);
    $profile_data[contact] = nl2br($profile_data[contact]);
    $profile_data[other] = nl2br($profile_data[other]);
    $smarty->assign('profile', $profile_data);
    $smarty->display('profile_view.tpl');

}

else {
    $smarty->assign('title', 'Ingen profil');
    $smarty->assign('message', 'Du er ikke innlogget, og dermed en anonym bruker.
            Hvis du ønsker, kan du <a href="session.php?action=login">logge inn</a> eller
            <a href="session.php?action=register">registrere deg</a> som ny bruker.');
    $smarty->display('info.tpl');
}

mysql_close($mysql_link);

?>
